package webBeans;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import beansInterfaces.BasicProductMgr;
import beansInterfaces.ConstraintsMgr;
import beansInterfaces.PackageMgr;
import dto.ResidenceDTO;
import dto.TransportDTO;

@ManagedBean(name="transportSearch")
@ViewScoped
public class TransportSearchBean implements Serializable{

	private static final long serialVersionUID = 1L;

	@EJB
	private BasicProductMgr basicProductMgr;
	@EJB
	private PackageMgr packageMgr;
	
	@EJB
	private ConstraintsMgr c;
	
	@ManagedProperty(value = "#{packageCreation}")
	private PackageCreationBean pBean;
	@ManagedProperty(value = "#{bookingBean}")
	private BookingBean uBean;
		
	private List<TransportDTO> transports;
	private List<TransportDTO> traUser;
	private TransportDTO currentTransport;
	private List<TransportDTO> filtered;
	private List<TransportDTO> filteredUser;
	
	private TransportDTO modifiedTransport;
	
		
	public TransportSearchBean() {}
	
	@PostConstruct
	public void init()
	{			 
	 	this.filtered = this.basicProductMgr.getAllTransportsDTO();
	 	this.filteredUser = basicProductMgr.getUserTransportDTO();
		this.transports=this.basicProductMgr.getAllTransportsDTO();
		this.traUser=this.basicProductMgr.getUserTransportDTO();
		
		this.modifiedTransport= new TransportDTO();

	}

	
	//Getters and Setters
	public TransportDTO getCurrentTransport() {
			return currentTransport;
		}

	public void setCurrentTransport(TransportDTO currentTransport) {
			this.currentTransport= currentTransport;
		}
		
	public List<TransportDTO> getTransports() {
		return transports;
	}

	public void setTransports(List<TransportDTO> transports) {
		this.transports = transports;
	}

	public List<TransportDTO> getFiltered() {
		return filtered;
	}

	public void setFiltered(List<TransportDTO> filtered) {
		this.filtered = filtered;
	}

	public void setpBean(PackageCreationBean pBean) {
		this.pBean = pBean;
	}
 
	public void setuBean(BookingBean uBean) {
		this.uBean = uBean;
	}
	
	public ConstraintsMgr getcMgr() {
		return c;
	}

	public void setcMgr(ConstraintsMgr cMgr) {
		this.c = cMgr;
	}
	
	public List<TransportDTO> getFilteredUser() {
		return filteredUser;
	}

	public void setFilteredUser(List<TransportDTO> filteredUser) {
		this.filteredUser = filteredUser;
	
	}
	
	
	
	
	public TransportDTO getModifiedTransport() {
		return modifiedTransport;
	}

	public void setModifiedTransport(TransportDTO modifiedTransport) {
		this.modifiedTransport = modifiedTransport;
	}

	//Employee Methods
	public String modifyTransport() 
	{	
		if(!this.packageMgr.isInPack(this.modifiedTransport)){
			
		this.basicProductMgr.updateTransport(modifiedTransport);
		return "/employee/indexBasicProduct?faces-redirect=true";
		}
		return "error";
	}
	
	public String deleteTransport() 
	{	
		if(!this.packageMgr.isInPack(currentTransport))
		{
			this.basicProductMgr.deleteTransport(currentTransport);
			this.pBean.resetCurrentPackage();
			return "indexBasicProduct";
		}
		return"error";	
		
	}
	
	public String addToPackAsDeparture()
	{
		if(this.currentTransport.getDisponibility()>0 && this.currentTransport.isAvailable())
		{
			this.pBean.getCurrentPackage().setDepartureTransport(this.currentTransport);
			this.pBean.getCurrentPackage().setPrice(this.c.autoSetPrice(this.pBean.getCurrentPackage()));
			this.currentTransport= new TransportDTO();
			return "createPackage";
		}
		return "error";
		
	}
	
	public String addToPackAsReturn()
	{
		if(this.currentTransport.getDisponibility()>0 && this.currentTransport.isAvailable())
		{
			this.pBean.getCurrentPackage().setReturnTransport(this.currentTransport);
			this.pBean.getCurrentPackage().setPrice(this.c.autoSetPrice(this.pBean.getCurrentPackage()));
			this.currentTransport= new TransportDTO();
			return "createPackage";

		}
		return "error";	
		
	}
	
	public void passToModified(){
		
		TransportDTO dto = this.currentTransport;
		this.modifiedTransport.setId(dto.getId());
		this.modifiedTransport.setDisponibility(dto.getDisponibility());
		this.modifiedTransport.setAdmin(this.uBean.getUserMgr().getUserDTO().getUsername());
		this.modifiedTransport.setPrice(dto.getPrice());
		this.modifiedTransport.setDepartureTime(dto.getDepartureTime());;
		this.modifiedTransport.setAvailable(dto.isAvailable());
		this.modifiedTransport.setType(dto.getType());
		this.modifiedTransport.setClassNumber(dto.getClassNumber());
		this.modifiedTransport.setDepartureDate(dto.getDepartureDate());
		this.modifiedTransport.setFromPlace(dto.getFromPlace());
		this.modifiedTransport.setToPlace(dto.getToPlace());
		this.modifiedTransport.setFlightId(dto.getFlightId());
		this.modifiedTransport.setPassengers(dto.getPassengers());
		this.modifiedTransport.setCompany(dto.getCompany());
	

}
	
	
	//User Methods

	
	public void changeDeparture()
	{
		if(this.currentTransport.getDisponibility()>0 && this.currentTransport.isAvailable())
		{
			this.uBean.getPackageSelected().setDepartureTransport(this.currentTransport);
			this.uBean.getPackageSelected().setPrice(this.c.autoSetPrice(this.uBean.getPackageSelected()));
			this.currentTransport= new TransportDTO();
		}
	}
	
	
	public void changeReturn()
	{
		if(this.currentTransport.getDisponibility()>0 && this.currentTransport.isAvailable())
		{
			this.uBean.getPackageOnEdit().setReturnTransport(this.currentTransport);
			this.uBean.getPackageOnEdit().setPrice(this.c.autoSetPrice(this.uBean.getPackageSelected()));
			this.currentTransport= new TransportDTO();
		}
	}

	public List<TransportDTO> getTraUser() {
		return traUser;
	}

	public void setTraUser(List<TransportDTO> traUser) {
		this.traUser = traUser;
	}
	
	
}

